package com.lepu.app.fun.grow.bean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;

public class PhotoItem {

	private static final String TABLE_NAME = "PhotoItem";

	public static final int UPDATE_YES = 10;//已同步
	public static final int UPDATE_NO = 11;//未同步
	
	public static final int MODIFY_YES = 20;//已需改
	public static final int MODIFY_NO = 21;//未修改

	public int id;// 数据库自增唯一标记
	
	public String uid;// 用户id
	public int sync;// 是否传到云端标记
	public String addTime;// 添加时间
	public String imagePath;// 图片地址
	public String desc;// 照片描述信息

	public int pictureId;// 服务端返回过来的id，判断是否修改
	
	public int modifyState;// 是否修改

	public static boolean insertPhotoItem(PhotoItem photoItem) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		ContentValues values = new ContentValues();
		values.put("Uid", photoItem.uid);
		values.put("Sync", photoItem.sync);
		values.put("AddTime", photoItem.addTime);
		values.put("ImagePath", photoItem.imagePath);
		values.put("Desc", photoItem.desc);

		values.put("PictureId", photoItem.pictureId);// 服务端返回的id，插入默认值0，上传成功后，更新这个id
		values.put("ModifyState", photoItem.modifyState);
		
		flag = dbHelp.insert(TABLE_NAME, null, values);

		return flag;
	}

	public static boolean updatePhotoItem(PhotoItem photoItem) {
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		ContentValues values = new ContentValues();
		values.put("Sync", photoItem.sync);
		values.put("AddTime", photoItem.addTime);
		values.put("ImagePath", photoItem.imagePath);
		values.put("Desc", photoItem.desc);
		values.put("ModifyState", photoItem.modifyState);

		flag = dbHelp
				.update(TABLE_NAME, values, "Uid=? and Id=?", new String[] {
						photoItem.uid, String.valueOf(photoItem.id) });

		return flag;
	}

	public static boolean updatePhotoItemSyncById(String uid,
			int NativePhotoID, int PhotoID) {
		
		boolean flag = false;

		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		ContentValues values = new ContentValues();
		values.put("Sync", PhotoItem.UPDATE_YES);
		values.put("PictureId", PhotoID);

		flag = dbHelp
				.update(TABLE_NAME, values, "Uid=? and Id=?", new String[] {
						uid, String.valueOf(NativePhotoID) });

		return flag;
	}

	public static boolean checkPhotoItem(String uid,
			int uniqueId) {
		boolean flag = false;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return flag;

		String sql = "select * from PhotoItem where Uid=? and Id=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid, String.valueOf(uniqueId) });

		if (queryResult != null && queryResult.size() > 0) {
			flag = true;
		}

		return flag;
	}
	
	/*
	 * 当前用户所有的照片数量
	 */
	public static int getAllPhotoCount(String uid) {
		
		int count = 0;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return count;

		String sql = "select count(*) from PhotoItem where Uid=?";
		String result =  dbHelp.executeScalar(sql,new String[]{uid});
		count = UtilityBase.parseInt(result);

		return count;
	}

	/*
	 * 当前用户是否有自己的图片相册
	 */
	public static ArrayList<PhotoItem> getPhotosByDate(String uid,
			String dateString) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from PhotoItem where Uid=? and AddTime=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid, dateString });

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<PhotoItem> dataList = new ArrayList<PhotoItem>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			PhotoItem item = PhotoItem.parseFromDatabase(dictionary);
			
			if(new File(item.imagePath).exists())
			{
				dataList.add(item);
			}
		}
		return dataList;
	}
	
	/*
	 * 当前用户所有的照片数量
	 */
	public static int getAllPhotosCount(String uid) {
		
		int count = 0;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return count;

		String sql = "select count(*) from PhotoItem where Uid=?";
		String result =  dbHelp.executeScalar(sql,new String[]{uid});
		count = UtilityBase.parseInt(result);

		return count;
	}
	
	public static ArrayList<PhotoItem> getAllPhotos(String uid) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from PhotoItem where Uid=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid });

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<PhotoItem> dataList = new ArrayList<PhotoItem>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			PhotoItem item = PhotoItem.parseFromDatabase(dictionary);
			if(new File(item.imagePath).exists())
			{
				dataList.add(item);
			}
		}
		return dataList;
	}

//	public static ArrayList<PhotoItem> getPhotosByTime(String uid,
//			String addTime) {
//		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
//		if (dbHelp == null)
//			return null;
//
//		String sql = "select * from PhotoItem where Uid=? and AddTime=?";
//		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
//				new String[] { uid, addTime });
//
//		if (queryResult == null || queryResult.size() == 0) {
//			return null;
//		}
//
//		ArrayList<PhotoItem> dataList = new ArrayList<PhotoItem>();
//		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
//			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
//			PhotoItem item = PhotoItem.parseFromDatabase(dictionary);
//			dataList.add(item);
//		}
//		return dataList;
//	}

	public static ArrayList<PhotoItem> getAllPhotosNoUpdate(String uid) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from PhotoItem where Uid=? and Sync=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid, String.valueOf(PhotoItem.UPDATE_NO) });

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<PhotoItem> dataList = new ArrayList<PhotoItem>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			PhotoItem item = PhotoItem.parseFromDatabase(dictionary);
			if(new File(item.imagePath).exists())
			{
				dataList.add(item);
			}
		}
		return dataList;
	}
	
	/*
	 * 当前用户所在天的照片数量
	 */
	public static int getPhotosByDateNoUpdateCount(String uid,String addTime) {
		
		int count = 0;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return count;

		String sql = "select count(*) from PhotoItem where Uid=? and Sync=? and AddTime=?";
		String result =  dbHelp.executeScalar(sql,new String[]{uid,String.valueOf(PhotoItem.UPDATE_NO),addTime});
		count = UtilityBase.parseInt(result);

		return count;
	}
	
	/*
	 * 当前用户所在天的照片数量
	 */
	public static int getPhotosByDateCount(String uid,String addTime) {
		
		int count = 0;
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return count;

		String sql = "select count(*) from PhotoItem where Uid=? and AddTime=?";
		String result =  dbHelp.executeScalar(sql,new String[]{uid,addTime});
		count = UtilityBase.parseInt(result);

		return count;
	}
	
	public static ArrayList<PhotoItem> getPhotosByDateNoUpdate(String uid,String addTime) {
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_ALBUM);
		if (dbHelp == null)
			return null;

		String sql = "select * from PhotoItem where Uid=? and Sync=? and AddTime=?";
		ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,
				new String[] { uid, String.valueOf(PhotoItem.UPDATE_NO),addTime });

		if (queryResult == null || queryResult.size() == 0) {
			return null;
		}

		ArrayList<PhotoItem> dataList = new ArrayList<PhotoItem>();
		for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
			HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
			PhotoItem item = PhotoItem.parseFromDatabase(dictionary);
			if(new File(item.imagePath).exists())
			{
				dataList.add(item);
			}
		}
		return dataList;
	}


	private static PhotoItem parseFromDatabase(
			HashMap<String, Object> dictionary) {
		if (dictionary == null || dictionary.size() == 0) {
			return null;
		}
		PhotoItem item = new PhotoItem();
		item.id = UtilityBase.parseInt((String) dictionary.get("Id"));
		item.uid = UtilityBase.parseString((String) dictionary.get("Uid"));
		item.sync = UtilityBase.parseInt((String) dictionary.get("Sync"));
		item.addTime = UtilityBase.parseString((String) dictionary
				.get("AddTime"));
		item.imagePath = UtilityBase.parseString((String) dictionary
				.get("ImagePath"));
		item.desc = UtilityBase.parseString((String) dictionary.get("Desc"));

		item.pictureId = UtilityBase.parseInt((String) dictionary
				.get("PictureId"));
		item.modifyState = UtilityBase.parseInt((String) dictionary
				.get("ModifyState"));
		return item;
	}
}
